﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.ServiceProcess;
using System.Collections;
using WaveMaster.Holidays.Club.Service;
using System.Configuration;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using System.Net;

namespace WaveMaster.Holidays.Club.ServiceServer
{
    class ServiceClient : System.ServiceProcess.ServiceBase
    {
        private static EventLog evt = new EventLog("Application");
        public static string SVC_NAME = "WaveMaster Client Service.";
        private System.Timers.Timer aTimer;
        private static ReportRequestService m_ReportRequestService;

        public ServiceClient()
		{
			this.ServiceName=SVC_NAME;
		}

        static void Main(string[] args)
        {
            evt.Source = SVC_NAME;
            evt.WriteEntry("WaveMaster Client Service Inizializing...");
            ServiceBase.Run(new ServiceClient());
        }

		protected override void OnStart(string[] args)
		{
            aTimer = new System.Timers.Timer(3000);
            aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
            aTimer.Enabled = true;
		}

        void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                string url = ConfigurationManager.AppSettings["URLServer"];
                m_ReportRequestService = Activator.GetObject(typeof(ReportRequestService), url) as ReportRequestService;


                string strHostName = System.Net.Dns.GetHostName(); ;
                IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
                IPAddress[] addr = ipEntry.AddressList;
                string ipaddress = addr[addr.Length - 1].ToString();
                if (ipaddress == "") { evt.WriteEntry("IP notdetected"); return; }

                IList reportBPdatas = m_ReportRequestService.GetBoardingPassRequestReportByIPAddress(ipaddress);
                string printerBP = ConfigurationManager.AppSettings["BoardingPassPrinterName"];
                string reportSourceBP = ConfigurationManager.AppSettings["BoardingPassReportFile"];
                foreach (DataSet bp in reportBPdatas)
                {
                    ReportDocument rpdoc = new ReportDocument();
                    rpdoc.Load(reportSourceBP);
                    rpdoc.SetDataSource(bp);
                    rpdoc.PrintOptions.PrinterName = printerBP;
                    rpdoc.PrintToPrinter(1, false, 1, 1);
                }

                IList reportWCdatas = m_ReportRequestService.GetWhiteCardRequestReportByIPAddress(ipaddress);
                string printerWC = ConfigurationManager.AppSettings["WhiteCardPrinterName"];
                string reportSourceWC = ConfigurationManager.AppSettings["WhiteCardReportFile"];
                foreach (DataSet wc in reportBPdatas)
                {
                    ReportDocument rpdoc = new ReportDocument();
                    rpdoc.Load(reportSourceWC);
                    rpdoc.SetDataSource(wc);
                    rpdoc.PrintOptions.PrinterName = printerWC;
                    rpdoc.PrintToPrinter(1, false, 1, 1);
                }
            }
            catch (Exception x)
            {
                evt.WriteEntry(x.Message);
            }
        }

		protected override void OnStop()
		{
            evt.WriteEntry("WaveMaster Client Service Stopped.");
		}
    }
}
